package com.duoketech.service.web.modulepic;
import com.duoketech.model.product.Product;
import com.duoketech.service.AbstractService;
import com.duoketech.model.modulepic.ModulePic;
import com.duoketech.exception.ValidateException;
import com.duoketech.utils.search.EqualCondition;
import com.duoketech.utils.search.LikeCondition;
import com.duoketech.utils.search.QueryConstructor;
import com.duoketech.utils.validator.Validator;
import com.duoketech.utils.validator.rules.string.StringLengthBetween;
import com.duoketech.utils.validator.rules.string.StringMustBeRequired;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Model;
import org.apache.commons.lang3.StringUtils;

public class ModulePicService extends AbstractService{
    public static ModulePicService ME = new ModulePicService();

    @Override
    public void validatorUpdate(Model<?> model, Kv kv) throws ValidateException {
        validatorAdd(model,kv);
    }

    @Override
    public void validatorAdd(Model<?> model, Kv kv) throws ValidateException {

        Validator validator = new Validator();
        validator.addRule(new StringMustBeRequired(model.getStr(ModulePic.NAME),"文件名称项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(ModulePic.NAME),"文件名称项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(ModulePic.TYPE),"图片类型项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(ModulePic.TYPE),"图片类型项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(ModulePic.COVER),"图片地址项为必填信息"));
        validator.addRule(new StringMustBeRequired(model.getStr(ModulePic.SORT),"排序项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(ModulePic.SORT),"排序项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(ModulePic.REMARK),"备注说明项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(ModulePic.REMARK),"备注说明项字符必须为1-50个字符",1,50));
        validator.excute();

    }

    @Override
    public void beforAdd(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterAdd(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforUpdate(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterUpdate(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforQuery(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterQuery(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforDelete(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterDelete(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public String tableName() {
        return ModulePic.dao.tableName();
    }

    @Override
    public void builderSQLExpected(Model<?> model, QueryConstructor queryConstruct, Kv kv) {
        queryConstruct.and(new EqualCondition(ModulePic.STATUS,ModulePic.STATUS_NORMAL));
        String value = model.getStr(ModulePic.NAME);
        if(StringUtils.isNotEmpty(value)){
            queryConstruct.and(new LikeCondition(ModulePic.NAME,"%"+value+"%"));
        }

    }
}